package com.yj;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ScheduleConfiguration {
    public static class DemoJob1Configuration {
        @Bean
        public JobDetail demoJob1() {
            return JobBuilder.newJob(DemoJob1.class)
                    .withIdentity("demoJob1")
                    .storeDurably() // 没有Trigger关联的时候任务是否被保留。因为创建JobDetail时，还没Trigger指向它，所以需要设置为true
                    .build();
        }

        @Bean
        public Trigger demoJob1Trigger() {
            // 简单的调度计划的构造器
            SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInSeconds(5) // 每5秒执行一次
                    .repeatForever(); // 无限循环
            // Trigger 构造器
            return TriggerBuilder.newTrigger()
                    .forJob(demoJob1())
                    .withIdentity("demoJob1Trigger")
                    .withSchedule(scheduleBuilder)
                    .build();
        }
    }

    public static class DemoJob2Configuration {
        @Bean
        public JobDetail demoJob2() {
            return JobBuilder.newJob(DemoJob2.class)
                    .withIdentity("demoJob2")
                    .storeDurably()
                    .build();
        }

        @Bean
        public Trigger demoJob2Trigger() {
            // 基于 Quartz Cron 表达式的调度计划的构造器
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ? *");
            // Trigger 构造器
            return TriggerBuilder.newTrigger()
                    .forJob(demoJob2())
                    .withIdentity("demoJob2Trigger")
                    .withSchedule(scheduleBuilder)
                    .build();
        }

    }
}
